const Com = require("../gmoon/baseCom");
cc.Class({
    extends: Com,

    properties: {

    },

    ctor(){
        this.__items = [];
    },

    onLoad(){
        //绑定拖拽
        this.bindDrag({
            start : {
                handler : this.onTouchStartEvent.bind(this) ,
                check : this.checkTouchStart.bind(this)
            },
            move : { handler : this.onTouchMoveEvent.bind(this) },
            end : { handler : this.onTouchEndEvent.bind(this) },
            cancel : { handler : this.onTouchEndEvent.bind(this) }
        });
    },

    checkTouchStart( _ , e ){
        return true 
    },

    onTouchStartEvent(e){
        let p = e ? e.getUserData() : {};
        console.log(p);
        // const curpos = cc.v2(p.pos.x , p.pos.y);
        // this.node.setPosition( curpos );
        
        // console.log(p);

        // p.callback = ( pback )=>{
        //     console.log('callback');
        // }
        // $EBUS.emit("gun-moving-evnet" , p );
    },

    onTouchMoveEvent(e){},

    onTouchEndEvent(e){},
    
    _addItem( item ){
        this.__items = this.__items || [];
        this.__items.push(item);
    },

    getEmptyItem( num ){
        num = num || 1;
        let re = [] , acc=0;
        for(let i=this.__items.length-1; i>=0 ; i--){
            if( !$T.getScr(this.__items[i]).getGun() ){
                re.push(this.__items[i]);
                acc++;
                if(acc>=num) break;
            }
        }
        return re.length==1? re[0] : (re.length<=0? null : re);
    },
    //调入元素
    loadItems(){
        const size = cc.winSize;
        const padding = 0 , padding_top = 0, margin_lr = 13 , margin_tb = 13 , nums = [ 5 , 5];
        const width = 110 ; //(size.width - padding*2 - (nums[0]-1)*padding) / nums[0];
        const height = 110;//(this.node.height-padding_top*2 - margin*(nums.length-1) ) / nums.length;

        let nodeSize , hb , x ,y , ars , wx=0;
        for(let i=0;i<nums.length; i++){
            for(let j=0; j<nums[i]; j++){
                hb = $RES.createPrefab("fab-hb-item");
                $T.setSprite( hb , $RES.g("img-hbitem-bg") , [width , height]);
                //$T.getScr(hb).setSize( width, height );
                
                x = $T.compListPoxV( 0 , j , width+margin_lr , nums[i] , -1);
                y = $T.compListPoxV( 0 , i , width+margin_tb , nums.length , -1);
                hb.parent = this.node;
                hb.x = x,hb.y=y;

                this._addItem(hb);
            }
        }
        nodeSize = $T.setNodeSize( this.node , [(width+margin_lr) * nums[0] , (height+margin_tb) * nums.length] );
        return nodeSize;
    },

    start () {

    },

    update (dt) {
    },
});
